﻿// ***********************************************************************
// Assembly         : SecurityLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="AccountActivationController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.Logging;

namespace InnovaApp.Layers.SecurityLayer.Controllers.ViewControllers.LayoutViewControllers
{
    /// <summary>
    /// Class AccountActivationController
    /// </summary>
    public class AccountActivationController : _LayoutViewControllers_Base<InnovaApp.Layers.SecurityLayer.Models.ViewModels._LayoutViewModels.AccountActivation_ViewModel>
    {
        /// <summary>
        /// Gets or sets the forms service.
        /// </summary>
        /// <value>The forms service.</value>
        public IFormsAuthenticationService FormsService { get; set; }
        /// <summary>
        /// Gets or sets the membership service.
        /// </summary>
        /// <value>The membership service.</value>
        public IMembershipService MembershipService { get; set; }

        /// <summary>
        /// Initializes data that might not be available when the constructor is called.
        /// </summary>
        /// <param name="requestContext">The HTTP context and route data.</param>
        protected override void Initialize(RequestContext requestContext)
        {
            base.eMethodBase = "InnovaApp.Layers.SecurityLayer.Controllers.RegisterController";
            base.specificCssCollection = new string[] { "Views/AccountCommon.css", "ClientComponents/KendoUI/kendo." + InnovaApp.Layers.CommonLayer.Controllers.Environment.KendoUITheme + ".min.css" };
            base.specificJSCollection = null;
            base.dynamicJSCollection = null;
            base.specificJqueryJSCollection = new string[] { "Views/AccountActivation.js" };
            base.externalCss = null;
            base.Meta.Add(enMeta.Description, Resources.View_AccountActivation_Meta_Description);
            base.Meta.Add(enMeta.Keywords, Resources.View_AccountActivation_Meta_Keywords);
            base.Meta.Add(enMeta.Subject, Resources.View_AccountActivation_Meta_Subject);

            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        /// <summary>
        /// URL[Get]: /AccountActivation
        /// </summary>
        /// <param name="codeactivation">The codeactivation.</param>
        /// <param name="email">The email.</param>
        /// <returns>ActionResult.</returns>
        public ActionResult Index(string codeactivation, string email)
        {
            using (DataRepositories _dataContext = new DataRepositories())
            {
                base.InitView_Common(
                    pageTitle: Resources.View_AccountActivation_PageTitle,
                    openingMessage: string.Empty,
                    specificCssCollection: specificCssCollection,
                    specificJSCollection: specificJSCollection,
                    dynamicJSCollection: dynamicJSCollection,
                    specificJqueryJSCollection: specificJqueryJSCollection,
                    jsVariables: jsVariables,
                    externalJs: externalJs,
                    externalCss: externalCss,
                    metaToAdd: Meta,
                    _dataContext: _dataContext,
                    displayMenus: false,
                    language: base.RequestLanguage,
                    adminMode: false,
                    usesKendoUI: true,
                    jQueryUITheme: null,
                    kendoUITheme: null,
                    currentUser: Membership.GetUser());
                try
                {
                    string username = Membership.GetUserNameByEmail(email);

                    if (username != null)
                    {
                        MembershipUser user = Membership.GetUser(username);
                        if (user != null)
                        {
                            if ((user.Email + user.CreationDate).EncodeSHA1() == codeactivation.Trim())
                            {
                                if (user.IsApproved == true)
                                {
                                    ContentData.ActivationResult = Resources.View_AccountActivation_AccountAlreadyActive;
                                }
                                else
                                {
                                    user.IsApproved = true;
                                    Membership.UpdateUser(user);
                                    ContentData.ActivationResult = Resources.View_AccountActivation_Success;
                                    StringBuilder oSB = new StringBuilder();
                                    oSB.Append("Account activation for user :");
                                    oSB.Append(user.UserName);
                                    oSB.AppendLine(" successful");
                                    Logger.Info(enLog.Business, oSB.ToString());
                                }
                            }
                            else
                            {
                                ContentData.ActivationResult = Resources.View_AccountActivation_AccountActivationCodeNotValid;
                            }
                        }
                        else
                        {
                            ContentData.ActivationResult = Resources.View_AccountActivation_UnknownAccount;
                        }
                    }
                    else
                    {
                        ContentData.ActivationResult = Resources.View_AccountActivation_UnknownEMail;
                    }
                }
                catch (Exception ex)
                {
                    ContentData.ActivationResult = Resources.View_AccountActivation_Exeption;
                }

            }
            return View(ContentData);
        }

    }
}
